//
//  HXDEmoticonToolBar.swift
//  weibo
//
//  Created by huxuedong on 15/12/15.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

//1.制定协议
protocol HXDEmoticonToolBarDelegate: NSObjectProtocol {
    func chooseButtonWithType(type: HXDEmoticonType)
}

//表情枚举
enum HXDEmoticonType: Int {
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}

class HXDEmoticonToolBar: UIStackView {

    //2.设置代理
    weak var delegate: HXDEmoticonToolBarDelegate?
    //当前选中的toolBar的按钮
    var currentSelectedButton: UIButton?
    //当前表情图片的组
    var currentSection: Int? {
        didSet {
            guard let section = currentSection else {
                return
            }
            //如果当前选中的按钮和图片的组数相同，则不做任何操作
            if currentSelectedButton?.tag == section + 1000 {
                return
            }
            //取出tag值与图片组数相同的按钮
            let button = viewWithTag(section + 1000) as! UIButton
            selectedButton(button)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //stackView的子控件的排列方式为水平排列
        axis = .Horizontal
        //stackView的子控件的布局方式为等距填充
        distribution = .FillEqually
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addEmoticonButton("最近", bgImage: "compose_emotion_table_left", type: .Recent)
        addEmoticonButton("默认", bgImage: "compose_emotion_table_mid", type: .Default)
        addEmoticonButton("Emoji", bgImage: "compose_emotion_table_mid", type: .Emoji)
        addEmoticonButton("浪小花", bgImage: "compose_emotion_table_right", type: .Lxh)
    }
    
    private func addEmoticonButton(title: String, bgImage: String, type: HXDEmoticonType) {
        let button = UIButton()
        button.addTarget(self, action: "emoticonButtonClick:", forControlEvents: .TouchUpInside)
        //获取枚举的真实值(1000-1003)
        button.tag = type.rawValue
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(13)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.setBackgroundImage(UIImage(named: "\(bgImage)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImage)_selected"), forState: UIControlState.Selected)
        addArrangedSubview(button)
        //设置程序启动默认选中"默认"按钮
        if type == .Default {
            button.selected = true
            currentSelectedButton = button
        }
        
    }
    
    @objc private func emoticonButtonClick(button: UIButton) {
        //如果正在点击的按钮和已经点击的按钮是同一个，则不做任何操作
        if currentSelectedButton == button {
            return
        }
        selectedButton(button)
        //3.调用代理方法
        delegate?.chooseButtonWithType(HXDEmoticonType(rawValue: button.tag)!)
    }
    
    //按钮的选择状态
    private func selectedButton(button: UIButton) {
        currentSelectedButton?.selected = false
        button.selected = true
        currentSelectedButton = button
    }

}
